/**
 * @description 配置岗位
 * @author 李艳
 */

import React, {Component} from 'react'
import connect from "react-redux/es/connect/connect";
import {show, hide} from '../../../configs/SharedReducer'
import TransferTable from "../../../commons/components/TransferTable";
import {organizationConfig} from "../../../configs/CommonComponentsConfig";
import { message } from 'antd';
import {
  insertEmployeePosition,
  listAllAssignedPositionsByUser, listAllCanAssignPositions, listAllCanAssignPositionsIncludeSubNode,
  removeEmployeePosition,
} from "./EnterpriseUserService";
import {listAllCanAssignEmployees} from "../Position/PositionService";

class ConfigPosition extends Component {
  handleLeftClick=async (rows)=> {
    let ids = [];
    const {userId}=this.props;
    for (let i = 0; i < rows.length; i++) {
      ids.push(rows[i].id);
    }
    await removeEmployeePosition({parentId:userId,childIds:ids}).then((data) => {
      const { status, message: msg } = data;
      if (status === 'SUCCESS') {
        message.success(msg);
      }
    }).catch(err => {
      this.props.hide()
    })

  }

  handleRightClick=async (rows)=> {
    let ids = [];
    const {userId}=this.props;
    for (let i = 0; i < rows.length; i++) {
      ids.push(rows[i].id);
    }
    await insertEmployeePosition({parentId:userId,childIds:ids}).then((data) => {
      const { status, message: msg } = data;
      if (status === 'SUCCESS') {
        message.success(msg);
      }
    }).catch(err => {
      this.props.hide()
    })
  };

  leftService = async (params) => {
    let result = [];
    const {organizationInfo,userId} = this.props;
    // if (!params.pageInfo){
    //   params.pageInfo={page:1,rows:defaultPageSize}
    // }
    if (!params.selectedKey){
      params.organizationId=organizationInfo?organizationInfo.id:""
    }else {
      params.organizationId=params.selectedKey;
      delete params.selectedKey
    }
    if (!params.includeSubNode){
      params.includeSubNode=false
    }
    params.userId=userId
    await listAllCanAssignPositions(params).then((res) => {
      result = res;
    });
    return result;

  };

  rightService = async (params) => {
    let result = [];
    const {userId} = this.props;
    await listAllAssignedPositionsByUser({parentId: userId}).then((res) => {
      result = res
    });
    return result;

  };
  JointQueryService = async (key, includeSubNode) => {
    const {organizationInfo,userId} = this.props;
    let params={};
    params.userId = userId;
    if (!key){
      params.organizationId=organizationInfo?organizationInfo.id:""
    }else {
      params.organizationId=key;
    }
    params.includeSubNode = includeSubNode;
    let result = [];
    await listAllCanAssignPositions(params).then((res) => {
      result = res
    });
    return result;
  };

  render() {
    const leftColumns = [
      {
        title: '代码',
        dataIndex: 'code',
        width: 150,
        render: (text,record) => {
          if(text){
            return <div title={text}>{text}</div>
          }
          return null;
        }
      },
      {
        title: '名称',
        dataIndex: 'name',
        width: 150,
        render: (text,record) => {
          if(text){
            return <div title={text}>{text}</div>
          }
          return null;
        }
      },
      {
        title: '组织机构',
        dataIndex: 'organization.namePath',
        width: 200,
        render: (text,record) => {
          if(text){
            return <div className={"text-short"} title={text}>{text}</div>
          }
          return null;
        }
      }
    ];
    const treeSelectConfig={
      ...organizationConfig,
      lable:"组织机构",
      defaultValue:this.props.organizationInfo?this.props.organizationInfo.id:""
    };

    const checkBoxConfig={lable:"包含子节点",service:listAllCanAssignPositionsIncludeSubNode}
    return (
      <TransferTable
        style={{paddingRight: 8, paddingBottom: 8, paddingLeft: 8}}
        treeSelectConfig={treeSelectConfig}
        checkBoxConfig={checkBoxConfig}
        handleLeftClick={this.handleLeftClick}
        handleRightClick={this.handleRightClick}
        rightService={this.rightService.bind(this)}
        leftService={this.leftService.bind(this)}
        leftColumns={leftColumns}
        rightColumns={leftColumns}
        JointQueryService={this.JointQueryService.bind(this)}
      />

    );
  }


}

const mapStateToProps = (state) => {
  return {};
}

const mapDispatchToProps = (dispatch) => {
  return {
    show: () => {
      dispatch(show())
    },
    hide: () => {
      dispatch(hide())
    }
  }
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(ConfigPosition)


